package ihooyah.com.whitehat.device;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import ihooyah.com.whitehat.dao.LitePalUtilKt;
import ihooyah.com.whitehat.utils.HYLocationUtil;
import org.litepal.LitePal;

import java.util.Locale;

/**
 * Created by debby on 2018/12/28.
 * 描述:设备管理器
 */
public class DeviceReceiver extends DeviceAdminReceiver {
    DeviceMethod method;

    @Override
    public void onEnabled(Context context, Intent intent) {
        // 设备管理：可用
        Toast.makeText(context, "设备管理：可用", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDisabled(final Context context, Intent intent) {
        // 设备管理：不可用
        Toast.makeText(context, "设备管理：不可用", Toast.LENGTH_SHORT).show();
        //如果取消了激活就再次提示激活
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getMethod(context).onActivate();
            }
        }, 3000);
    }


    private DeviceMethod getMethod(Context context) {
        if (method == null) {
            method = new DeviceMethod(context.getApplicationContext());
        }
        return method;
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
           /* // 这里处理 不可编辑设备。这里可以造成死机状态
            Intent intent2 = new Intent(context, NoticeSetting.class);
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
            context.stopService(intent);// 是否可以停止*/
        return "这是一个可选的消息，警告有关禁止用户的请求";
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        // 设备管理：密码己经改变
        Toast.makeText(context, "设备管理：密码己经改变", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        // 设备管理：改变密码失败
        Toast.makeText(context, "设备管理：改变密码失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        // 设备管理：改变密码成功
        Toast.makeText(context, "设备管理：改变密码成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.e("onMyReceive", "onTimerReceive" + context.getPackageName() + intent.getAction());
        if (intent.getAction() == null) {
            return;
        }
        //发送唤醒广播来促使挂掉的UI进程重新启动起来
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent();
        alarmIntent.setAction("com.ihooyah.action.endurance");
        PendingIntent operation = PendingIntent.getBroadcast(context,
                2, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setWindow(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + 10 * 1000, 0, operation);

        final HYLocationUtil instance = new HYLocationUtil(context);
        instance.startLocation(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation == null) {
                    return;
                }
                //海拔
                String text = String.format(Locale.CHINA,
                        "海拔%.0f米，经纬度为=%.6f,%.6f"
                        , bdLocation.getAltitude()
                        , bdLocation.getLongitude()
                        , bdLocation.getLatitude());
                Log.e("onMyReceive", text);
                LitePal.initialize(context);
                LitePalUtilKt.saveLocationDAO(bdLocation.getLongitude(),
                        bdLocation.getLatitude(),
                        bdLocation.getLocationDescribe());
                instance.closedLocation();
            }
        });
    }
}
